/*
 * DownloadAddBox2.java
 *
 * Created on 20 de Fevereiro de 2008, 1:14
 */

package downloadmanager;

import java.awt.Component;
import java.io.File;
import java.util.GregorianCalendar;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFileChooser;
import javax.swing.SpinnerNumberModel;
import org.jdesktop.application.Action;

/**
 *
 * @author  MX
 */
public class DownloadAddBox2 extends javax.swing.JDialog {
    
    private GregorianCalendar _calendar = new GregorianCalendar();
    private Download _file;
    
    /** Creates new form DownloadAddBox */
    public DownloadAddBox2(java.awt.Frame parent, Download f) {
        super(parent);
        initComponents();

        // Update current file of work
        _file = f;
        updateFilePath();
        
        // Update Schedule fields
        spinnerHours.setValue(_calendar.get(_calendar.HOUR_OF_DAY));
        spinnerMinutes.setValue(_calendar.get(_calendar.MINUTE));
    }
    
    @Action public void updateFilePath()
    {
        textDestination.setText(_file.getDestination().getAbsolutePath());
    }
    
    @Action public void closeDownloadAddBox() {
        setVisible(false);
    }

    @Action public void doDownloadAdd() {
        if (checkboxSchedule.isSelected())
        {
            _file.setScheduleTime((Integer) spinnerHours.getValue(), (Integer) spinnerMinutes.getValue());
        }

        App.getApplication().getDownloadsModel().add(_file);
        closeDownloadAddBox();
    }
    
    @Action public void doBrowse()
    {
        JFileChooser fc = createFileBrowser(_file.getDestination());
        int option = fc.showOpenDialog(this);
        if (JFileChooser.APPROVE_OPTION == option) {
            _file.setDestination(fc.getSelectedFile());
            updateFilePath();
        }
    }

    private JFileChooser createFileBrowser(File f) {
        JFileChooser fc = new JFileChooser(f);
        fc.setDialogTitle("Guardar Como");
        return fc;
    }
    
    @Action public void toggleScheduleBox() {
        for (Component c : panelSchedule.getComponents())
        {
            c.setEnabled(checkboxSchedule.isSelected());
        }
    }

    private class HoursSpinnerModel extends SpinnerNumberModel {
        @Override public void setValue(Object value) {
            if ((Integer) value > 23)
            {
                super.setValue(0);
            }
            else
            if ((Integer) value < 0)
            {
                super.setValue(23);
            }
            else
            {
                super.setValue(value);
            }
        }
    }
    
    private class MinutesSpinnerModel extends SpinnerNumberModel {

        public MinutesSpinnerModel() {
            super(0, -1, 60, 1);
        }
        
        @Override public void setValue(Object value) {
            if ((Integer) value > 59)
            {
                super.setValue(0);
                spinnerHours.getModel().setValue(spinnerHours.getModel().getNextValue());
            }
            else
            if ((Integer) value < 0)
            {
                super.setValue(59);
                spinnerHours.getModel().setValue(spinnerHours.getModel().getPreviousValue());
            }
            else
            {
                super.setValue(value);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        textDestination = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        checkboxSchedule = new javax.swing.JCheckBox();
        panelSchedule = new javax.swing.JPanel();
        spinnerHours = new javax.swing.JSpinner();
        spinnerMinutes = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        buttonCancel = new javax.swing.JButton();
        buttonOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(downloadmanager.App.class).getContext().getResourceMap(DownloadAddBox2.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        textDestination.setText(resourceMap.getString("textDestination.text")); // NOI18N
        textDestination.setName("textDestination"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(downloadmanager.App.class).getContext().getActionMap(DownloadAddBox2.class, this);
        jButton1.setAction(actionMap.get("doBrowse")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        checkboxSchedule.setAction(actionMap.get("toggleScheduleBox")); // NOI18N
        checkboxSchedule.setText(resourceMap.getString("checkboxSchedule.text")); // NOI18N
        checkboxSchedule.setName("checkboxSchedule"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkboxSchedule)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(textDestination, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(textDestination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(checkboxSchedule)
                .addContainerGap())
        );

        panelSchedule.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("panelSchedule.border.title"))); // NOI18N
        panelSchedule.setName("panelSchedule"); // NOI18N

        spinnerHours.setModel(new HoursSpinnerModel());
        spinnerHours.setEnabled(false);
        spinnerHours.setName("spinnerHours"); // NOI18N

        spinnerMinutes.setModel(new MinutesSpinnerModel());
        spinnerMinutes.setEnabled(false);
        spinnerMinutes.setName("spinnerMinutes"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout panelScheduleLayout = new javax.swing.GroupLayout(panelSchedule);
        panelSchedule.setLayout(panelScheduleLayout);
        panelScheduleLayout.setHorizontalGroup(
            panelScheduleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelScheduleLayout.createSequentialGroup()
                .addContainerGap(286, Short.MAX_VALUE)
                .addComponent(spinnerHours, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spinnerMinutes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2))
        );
        panelScheduleLayout.setVerticalGroup(
            panelScheduleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(spinnerMinutes, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
            .addComponent(spinnerHours, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
            .addComponent(jLabel1)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        buttonCancel.setAction(actionMap.get("closeDownloadAddBox")); // NOI18N
        buttonCancel.setText(resourceMap.getString("buttonCancel.text")); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N

        buttonOK.setAction(actionMap.get("doDownloadAdd")); // NOI18N
        buttonOK.setText(resourceMap.getString("buttonOK.text")); // NOI18N
        buttonOK.setName("buttonOK"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(229, Short.MAX_VALUE)
                .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonCancel)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(buttonCancel)
                .addComponent(buttonOK))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelSchedule, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelSchedule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOK;
    private javax.swing.JCheckBox checkboxSchedule;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel panelSchedule;
    private javax.swing.JSpinner spinnerHours;
    private javax.swing.JSpinner spinnerMinutes;
    private javax.swing.JTextField textDestination;
    // End of variables declaration//GEN-END:variables
    
}
